package com.example.demo.api.controller.v1;

import com.example.demo.api.dto.EmployeeDTO;
import com.example.demo.domain.entity.Employee;
import com.example.demo.service.EmployeeService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/employee")
public class EmployeeController {
    private final EmployeeService employeeService;

    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    /**
     * 一对一查询，可以查到用户及其部门详情
     * @param employeeDTO
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<?> list(EmployeeDTO employeeDTO){
        List<Employee> list = employeeService.findEmployeeByCondition(employeeDTO);
        return ResponseEntity.ok(list);
    }

    @GetMapping("/{employeeId}")
    public ResponseEntity<?> getById(@PathVariable("employeeId") Long employeeId){
        System.out.println(employeeId);
        Employee employee = employeeService.findEmployeeById(employeeId);
        return ResponseEntity.ok(employee);
    }

    @GetMapping("/len")
    public ResponseEntity<?> getLen(){
        int len = employeeService.countEmployeeLen();
        return ResponseEntity.ok(len);
    }

    @PutMapping("/update")
    public ResponseEntity<?> updateEmployee(@RequestBody EmployeeDTO employeeDTO) {
        if (employeeDTO.getEmployeeId() == null) {
            return ResponseEntity.ok("[employee_update]update error!");
        }
//        System.out.println(111);
        employeeService.updateEmployee(employeeDTO);
        return ResponseEntity.ok(null);
    }


    @PutMapping("/insert")
    public ResponseEntity<?> insertEmployee(@RequestBody EmployeeDTO employeeDTO) {
        if (employeeDTO.getEmployeeId() == null) {
            return ResponseEntity.ok("[employee_insert]insert error!");
        }
        employeeService.insertEmployee(employeeDTO);
        return ResponseEntity.ok(null);
    }

    @DeleteMapping("/delete")
    public ResponseEntity<?> deleteEmployee(@RequestParam Long employeeId){
        if (employeeId == null) {
            return ResponseEntity.ok("[employee_delete]delete error!");
        }
        employeeService.removeEmployee(employeeId);
        return ResponseEntity.ok(null);
    }


}
